package com.xone.xapp;

import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.GridView;
import android.widget.Toast;

public class LinksActivity extends BasicActivity {
	
	protected GridView linksGridView;

	@Override
	public String getHeaderTitle() {
		return "定制快捷方式";
	}

	@Override
	public int getContentView() {
		return R.layout.links;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		linksGridView = (GridView)findViewById(R.id.links_grid_view);
		//下部的图片行为框部分
		Resources r = getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Constants.GRID_PADDING, r.getDisplayMetrics());
		int columnWidth = (int) ((ContextUtils.getScreenWidth(this) - ((Constants.NUM_OF_COLUMNS + 1) * padding)) / Constants.NUM_OF_COLUMNS);
		linksGridView.setNumColumns(Constants.NUM_OF_COLUMNS);
		linksGridView.setColumnWidth(columnWidth);
		linksGridView.setStretchMode(GridView.NO_STRETCH);
		linksGridView.setPadding((int)padding/2, (int) padding/2, (int)padding/2, (int) padding/2);
		linksGridView.setHorizontalSpacing((int) padding);
		linksGridView.setVerticalSpacing((int) padding);
		showProgress();
		new InitDataTask(this, linksGridView, columnWidth).execute();
	}
	
	/**
	 * 取向导的图片默认图片
	 */
	public class InitDataTask extends AsyncTask<String, Integer, List<JSONObject>> {
		protected Context context;
		protected GridView gridView;
		protected int imageWidth;
		public InitDataTask(Context context, GridView gridView, int imageWidth) {
			this.context = context;
			this.gridView = gridView;
		}
		@Override
		protected List<JSONObject> doInBackground(String... params) {
			if (isCancelled()) {
				return null;
			}
			return HttpUtils.httpGet(Constants.APP_URL + "/assistant/linkJsonkeys.html", null, new HttpUtils.onGetSuccessListener<List<JSONObject>>() {
				@Override
				public List<JSONObject> onSuccess(int status, String result) {
					if (HttpUtils.SC_OK == status) {
						ShareUtils.setPrefs("links_setting", result, context);
						return JSONUtils.getJSONListFromIdentifyString(result, "links");
					} else {
//						Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
					}
					return null;
				}
			}, context);
		}
		@Override
		protected void onPostExecute(List<JSONObject> result) {
			super.onPostExecute(result);
			hideProgress();
			if (null == result || result.isEmpty()) {
				List<JSONObject> ls = JSONUtils.getJSONListFromIdentifyString(ShareUtils.getPrefs("links_setting", context), "links");
				if (null != ls && !ls.isEmpty()) {
					result = ls;
				} else {
					Toast.makeText(context, "未能加载到数据", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			LinksGridAdapter linksGridAdapter = new LinksGridAdapter(context, result, imageWidth);
			gridView.setAdapter(linksGridAdapter);
		}
	}

}
